package com.controller;

import com.entity.BGoods;
import com.entity.BType;
import com.entity.table.BGoodsTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.BGoodsServiceImpl;
import com.service.impl.BTypeServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Resource
    BGoodsServiceImpl bGoodsService;
    @Resource
    BTypeServiceImpl bTypeService;

    @GetMapping("")
    public String index(Model model) {
        try {
            Map<String, List<BGoods>> goodsList = new LinkedHashMap<>();
            //查询商品列表分类
            QueryWrapper wrapper = QueryWrapper.create().limit(6);
            List<BType> typeList = bTypeService.getMapper().selectListByQuery(wrapper);
            for (BType bType : typeList) {
                int typeId = bType.getId();
                String typeName = bType.getTypeName();
                //查询商品列表
                wrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.TYPE_ID.eq(typeId)).limit(4);
                List<BGoods> goods = bGoodsService.list(wrapper);
                goodsList.put(typeName, goods);
            }
            model.addAttribute("goodType", typeList);
            model.addAttribute("goodsList", goodsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "client/index";
    }

    //商品列表
    @GetMapping("/list")
    public String goodList(ModelMap model, @RequestParam(value = "goodName", defaultValue = "") String goodName,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        try {
            QueryWrapper queryWrapper;
//            if (goodName.equals("")) {
//                queryWrapper = QueryWrapper.create();
//            } else {
//                queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
//            }
            queryWrapper = QueryWrapper.create().where(BGoodsTableDef.B_GOODS.GOOD_NAME.like(goodName));
            Page<BGoods> page = new Page<>();
            page.setPageSize(pageSize);
            page.setPageNumber(p);
            Page<BGoods> pageInfo = bGoodsService.getMapper().paginateWithRelations(page, queryWrapper);
            System.out.println("检查属性值是否为 null : " + pageInfo);
//            String typename = pageInfo.getRecords().get(0).getBType().getTypeName(); //无用代码
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        } catch (Exception e) {
            e.printStackTrace();
            return "client/login";
        }
        return "client/list";
    }

    //修改商品页面
    @GetMapping("/goodDetail")
    public String goodDetail(int id, Model model) {
        try {
            //查询商品分类列表

            //根据id查询商品
            QueryWrapper queryWrapper =  QueryWrapper.create().where(BGoodsTableDef.B_GOODS.ID.eq(id));
            BGoods bGoods = bGoodsService.getMapper().selectOneByQuery(queryWrapper);
            //返回商品类型与商品数据
            model.addAttribute("bGood", bGoods);
            model.addAttribute("bGood", bGoods);
        } catch (Exception e) {
            e.printStackTrace();
            return "client/login";
        }
        return "client/detail";
    }

}
